package org.nabc.generate.mapper.xml;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.nabc.configuration.FileConfiguration;
import org.nabc.constant.ModeConstant;
import org.nabc.constant.TypeConstant;
import org.nabc.generate.AbstractRunnableTask;
import org.nabc.support.PropertyValue;
import org.nabc.support.PropertyValues;

@Slf4j
public class MybatisPlusMapperXmlTemplateGenerate extends AbstractRunnableTask {
    @Override
    public void setParameters(FileConfiguration fileConfiguration, PropertyValues propertyValues) {
        this.fileSuffix = "xml";
        fileTemplateData = inputFromFile("src/main/resources/template/mybatis-plus/mapper/xml/MapperXml.template");

        String[] strings = getTemplate("#\\[[\\W*\\w*]{0,}?\\]#", fileTemplateData.toString());

        super.setParameters(fileConfiguration, propertyValues, "xml");
    }

    @Override
    public void setModelAndType() {
        this.type = TypeConstant.MAPPER_XML;
        this.mode = ModeConstant.MYBATIS_PLUS;
    }

    @SneakyThrows
    @Override
    public void generate() {
        String xmlFileData = fileTemplateData.toString()
                .replace("#{primitiveTableName}", propertyValues.getPrimitiveTableName())
                .replace("#{packageName}", fileConfiguration.getPackageName())
                .replace("#{entityClass}", entityName)
                .replace("#{entityName}", lowerLetter(entityName))
                .replace("#{columnName}", lowerLetter(entityName));

        outputToFile(xmlFileData, filePath);
    }
}
